<?php


namespace app\api\controller\v1;
use app\api\model\v1\MechanismBanner;
use app\api\model\v1\MechanismCourse;
use app\api\model\v1\MechanismMember;
use app\api\model\v1\MechanismWork;
use app\api\model\v1\StudyBanner;
use app\api\model\v1\StudyMember;
use app\api\model\v1\StudyWork;
use app\api\model\v1\UserCheck;
use app\api\model\v1\UserStudy;
use app\common\controller\Api;


class Study extends Api
{
    protected $noNeedLogin = ['mechanismList','mechanismDetail','mechanismCourseList'];
    protected $noNeedRight = ['*'];

    /**
     * 学习中心列表
     */
    public function mechanismList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $category_id = $this->request->post('category_id'); //分类ID
        $province_name = $this->request->post('province_name'); //省
        $city_name = $this->request->post('city_name'); //市
        $district_name = $this->request->post('district_name'); //区
        $category = $this->request->post('category'); //所属分类
        $map = $map2 = [];
        if ($category_id) {
            $map2 = "FIND_IN_SET($category_id,courses_category_ids)";
        }
        if ($province_name) {
            $map['province_name'] = ['=', $province_name];
        }
        if ($city_name) {
            $map['city_name'] = ['=', $city_name];
        }
        if ($district_name) {
            $map['district_name'] = ['=', $district_name];
        }
        if ($category) {
            $map['category'] = ['=', $category];
        }
        $map['status'] = ['=', 1];
        $list = UserStudy::with(['user'=>function($query) {
            $query->field('id,avatar');
        }])->where($map)->where($map2)->order('id desc')->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

    /**
     * 学习中心详情
     */
    public function mechanismDetail()
    {
        $id = $this->request->post('id');
        $checkModel = UserStudy::where('id', $id)->where('status', 1)->find();
        if (!$checkModel) $this->error('学习中心不存在');
        $work = StudyWork::where('user_study_id', $id)->order('weigh asc')->field('image')->select();
        $member = StudyMember::where('user_study_id', $id)->order('weigh asc')->select();
        $this->success('成功', ['mechanism'=>$checkModel,'work'=>$work,'member'=>$member]);
    }
    
    /**
     * 学习中心课程列表
     */
    public function mechanismCourseList()
    {
        $limit = $this->request->post('limit') ?? 10;
        $id = $this->request->post('id'); //学习中心ID
        $category_id = $this->request->post('category_id'); //分类ID
        $sold_sort = $this->request->post('sold_sort'); //销量排序desc(倒序)asc(正序)
        $id_sort = $this->request->post('id_sort'); //最新排序desc(倒序)asc(正序)
        $mechanism_category_topid = $this->request->post('mechanism_category_topid'); //专业ID
        $mechanism_category_id = $this->request->post('mechanism_category_id'); //级别ID
        $studyModel = UserStudy::where('id', $id)->find();
        if (!$studyModel) $this->error('学习中心不存在');
        $map = [];
        $order = 'id desc';
        if ($category_id) {
            $map['pid'] = ['=', $category_id];
        }
        if ($sold_sort) {
            $order = "sales_num $sold_sort";
        }
        if ($id_sort) {
            $order = "id $id_sort";
        }
        if ($mechanism_category_topid) {
            $map['mechanism_category_top'] = ['=', $mechanism_category_topid];
        }
        if ($mechanism_category_id) {
            $map['mechanism_category_two'] = ['=', $mechanism_category_id];
        }
        $user_check_ids = explode(',', $studyModel->user_check_ids);
        $map['user_check_id'] = ['in', $user_check_ids];
        $list = MechanismCourse::where($map)->order($order)->paginate($limit)->toArray();
        $this->success('成功', $list);
    }

}